package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.entity.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 支付表 前端控制器
 * </p>
 *
 * @author 小吁
 * @since 2021-04-13
 */
@RestController
@Slf4j
public class PaymentController {

    @Autowired
    PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;  //获取当前服务的端口

    @Autowired
    private DiscoveryClient discoveryClient;


    @PostMapping("payment/create")
    public CommonResult<Payment> createPayment(@RequestBody Payment payment){
        boolean isSave = paymentService.save(payment);
        if (isSave) {
            //日志打印
            log.info("存入成功! 存入的数据为:"+payment);
            //返回json
            return new CommonResult<>(200,"信息已成功存入数据库,serverPort为:"+serverPort,payment);
        }else {
            log.error("存入失败!");
            return  new CommonResult<>(400,"数据异常,无法存入数据库");
        }
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getById(id);
        if (payment!=null) {
            log.info("查询成功! 查询的数据为:"+payment);
            return new CommonResult<>(200,"查询成功! serverPort为:"+serverPort,payment);
        }else{
            log.error("查询失败!");
            return new CommonResult<>(400,"数据库无id为:"+id+"的数据");
        }
    }

    //服务发现功能的使用
    @GetMapping("/payment/discovery")
    public Object discovery(){
        //获取所有注册过的服务
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("serverPort:"+serverPort+"--eureka中的服务service:"+service);
        }
        //获取指定服务下的所有运行实例
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info("serverPort:"+serverPort+"--eureka中的服务id为CLOUD-PAYMENT-SERVICE的实例:"+instance.getServiceId()+"--"+instance.getHost()+
                    "--"+instance.getUri()+"--"+ instance.getPort());
        }
        return this.discoveryClient;
    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB() {
        return serverPort;
    }


    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    @GetMapping("/payment/zipkin")
    public String paymentZipkin() {
        return "hi ,i'am paymentzipkin server fall back，welcome to here, O(∩_∩)O哈哈~";
    }

}

